using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_DiaDiemUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            int iD=int.Parse (Request .QueryString ["iD"].ToString ());
            txt_madiadiem .Text =iD.ToString ();
            tbDiaDiem diadiem=tbDiaDiemBUS .GetByID (iD);
            txt_tendiadiem .Text=diadiem .TenDD  ;
            txt_diachi .Text=diadiem .DiaChi  ;
            txt_dienthoai .Text=diadiem .DienThoai  ;
            img_bando.ImageUrl = "~/Photos/Thumb/" + diadiem.BanDo;

        }
        catch
        {
            Response.Redirect("~/Admin/DiaDiemDanhSach.aspx");
        }

    }
    protected void btn_sua_Click(object sender, EventArgs e)
    {
        try
        {
            int iD = int.Parse(Request.QueryString["iD"].ToString());
            tbDiaDiem  diadiem = tbDiaDiemBUS.GetByID(iD);
            diadiem.TenDD = txt_tendiadiem .Text ;
            diadiem.DiaChi=txt_diachi .Text ;
            diadiem.DienThoai=txt_dienthoai .Text ;
            if (fu_bando.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_bando.FileName;
                fu_bando.SaveAs(Server.MapPath("~/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));
                ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                if (diadiem.BanDo != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + diadiem.BanDo));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + diadiem.BanDo));
                }
                diadiem.BanDo = filename;
            }
            tbDiaDiemBUS.Update(diadiem);
        }
        catch
        {
            Response.Redirect("~/Admin/DiaDiemDanhSach.aspx");
        }
        Response.Redirect("~/Admin/DiaDiemDanhSach.aspx");
        
        Response.Redirect(Request.RawUrl);
    }
}

